package dem2;

import dem1.BinaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-04-21
 * Time: 23:02
 */
public class Test1 {

    static class TreeNode {
        public int val;
        public Test1.TreeNode left;//左孩子的引用
        public Test1.TreeNode right;//右孩子的引用

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode creatTree(){
        TreeNode tree1 = new TreeNode(1);
        TreeNode tree4 = new TreeNode(4);
        TreeNode tree3 = new TreeNode(3);
        TreeNode tree2 = new TreeNode(2);
        tree1.left = tree4;
    }

    public List<Integer> preorderTraversal(Test1.TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Queue<Test1.TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            Test1.TreeNode node = queue.poll();
            if(node != null){
                queue.offer(node.left);
                queue.offer(node.right);
                list.add(node.val);
            }
        }
        return list;
    }

}
